package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @CacheEvict(cacheNames = "dish",key = "#dto.categoryId")
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dto){
        log.info("新增菜品：{}",dto);
        dishService.save(dto);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> query(DishPageQueryDTO dto){
        log.info("菜品分页查询:{}",dto);
        PageResult pageResult = dishService.query(dto);
        return Result.success(pageResult);
    }

    /**
     * 根据ID删除菜品
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "dish",allEntries = true)
    @ApiOperation("根据ID删除菜品")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("根据ID删除菜品:{}",ids);
        dishService.delete(ids);
        return Result.success();
    }

    /**
     * 根据ID查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据ID查询菜品：{}",id);
        DishVO vo = dishService.getByIdWithFlavor(id);
        return Result.success(vo);
    }

    /**
     * 修改菜品
     * @param dto
     * @return
     */
    @CacheEvict(cacheNames = "dish",allEntries = true)
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dto){
        log.info("修改菜品:{}",dto);
        dishService.update(dto);
        return Result.success();
    }

    /**
     * 菜品起售、停售
     * @return
     */
    @CacheEvict(cacheNames = "dish",allEntries = true)
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售、停售")
    public Result status(@PathVariable("status") Integer status, Long id){
        log.info("菜品起售，停售:{}{}",status ,id);
        dishService.status(status,id);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> list(Long categoryId){
        log.info("根据分类id查询菜品:{}",categoryId);
        List<DishVO> vo = dishService.list(categoryId);
        return Result.success(vo);
    }
}
